<template>
  <vue-content>
    <vue-header>
      <!-- <vue-header-form>
        <el-form :inline="true" :model="formFilter" ref="formFilter">
        	<el-form-item label="商品类型" prop="productType">
            <el-select v-model="formFilter.productType" style="width:150px">
              <el-option label="实物" value="1"></el-option>
              <el-option label="券/虚拟" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品名称" prop="productName">
            <el-input v-model.trim="formFilter.productName" style="width:150px"></el-input>
          </el-form-item>
          <el-form-item label="商品类别" prop="categoryNo">
            <el-select v-model="formFilter.categoryNo" style="width:150px">
              <el-option 
              	v-for="item in CategoryList"
      					:key="item.id"
      					:label="item.categoryName"
      					:value="item.categoryNo"
      				></el-option>
        </el-select>
          </el-form-item>
          <el-form-item>
            
            <el-button @click="exportForm('formFilter')">导出</el-button>
          </el-form-item>
        </el-form>
      </vue-header-form> -->
      <!-- <div style="float:right;padding-bottom:15px">
        <el-button type="info" @click="SearchSubmit('formFilter')">查询</el-button>
        <el-button @click="resetForm('formFilter')">重置</el-button>
      </div> -->
    </vue-header>
    <vue-body>
      <el-table
      	v-loading.body="loading"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="name"
          label="优惠券名称">
        </el-table-column>
        <el-table-column
          label="类型">
          <template slot-scope="scope">
            {{ scope.row.type | type }}
          </template>
        </el-table-column>
        <el-table-column
          label="发券途径">
          <template slot-scope="scope">
            {{ scope.row.promAppro | promAppro }}
          </template>
        </el-table-column>
        <el-table-column
          prop="denomination"
          label="面值">
        </el-table-column>
        <el-table-column
          prop="validatyBegin"
          label="有效期开始日期">
        </el-table-column>
        <el-table-column
          prop="validatyEnd"
          label="有效期结束日期">
        </el-table-column>
        <el-table-column
          prop="useTime"
          label="限制使用时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="info"
              @click="handleDetail(scope.$index, scope.row, tableData)">详情</el-button>
          </template>
      </el-table-column>
      </el-table>
    </vue-body>
     <vue-footer style="padding: 5px 0;margin: 0 -5px;float:right;">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="pageSize">
      </el-pagination>
    </vue-footer>
    <!-- <edit-role
      ref="edit-role"
      @close="editFinish"
      :view="view"
      v-if="view ==='create' || view ==='edit'"/> -->
    <edit-detail
      ref="edit-detail"
      @close="editFinish"
      :view="view"
      v-if="view ==='detail'"/>
  </vue-content>
</template>

<script>
  import { md5 } from '@/utilities/crypto' // 加密
  import AuthStorage from '@/services/storage.js'
  import {getCouponReport} from '@/services/reports'
  import {RequestError} from '@/services/errors'
  import {deepClone} from '@/utilities/object'
  import EditRole from './edit'
  import EditDetail from './Detail'
  export default {
    name: 'role-table',
    components: {EditRole,EditDetail},
    data: () => ({
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      formFilter: {
      },
      loading: false,
      CategoryList:[],
      view: '',
      ifShow:false
    }),
    methods: {
      async loadData () {
        this.loading = true
        try {
          let data = await getCouponReport(Object.assign({currentPage: this.currentPage},{perPage:10},this.formFilter))
          // debugger
          this.tableData = data.data.couponList
          this.pageSize = parseInt(data.totalResult)
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async exportData () {
        var param=Object.assign({currentPage: this.currentPage},{perPage:10},this.formFilter);
        var  a = new Date();
        var btime= a .getTime();
        var signs2 = "data="+JSON.stringify(param)+"&stmb="+btime;
        this.postDownLoadFile({
          url:this.$store.state.exportUrl + 'ecshop/console/statement/statisticsProductExport.do?stmb='+ a.getTime() +'&source="03"&token='+ AuthStorage.token +'&version=""&data='+JSON.stringify(param)+'&sign='+this.signature(signs2),
          method:'post'
        });
      },
      async handleDetail (index, data) {
        this.view = 'detail'
        await this.$nextTick()
        this.$refs['edit-detail'].couponId = data.couponId
        this.$refs['edit-detail'].loadData()
      },
      signature(data) {
        return md5(data)
      },
      SearchSubmit () {
        this.loadData()
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
        this.currentPage = 1
        this.loadData()
      },
      exportForm(){
        this.exportData();
      },
      postDownLoadFile(options) {
          var config = $.extend(true, { method: 'post' }, options);
          var $iframe = $('<iframe id="down-file-iframe" />');
          var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
          $form.attr('action', config.url);
          for (var key in config.data) {
              $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
          }
          $iframe.append($form);
          $(document.body).append($iframe);
          $form[0].submit();
          $iframe.remove();
      },
      editFinish (needRefresh) {
        this.view = ''
        if (needRefresh) {
          this.loadData()
        }
      }
    },
    watch: {
      currentPage (newVal) {
        this.loadData(newVal)
      }
    },
    filters: {
      promAppro (value) {
      	//发券途径
        return {'general': '普通券','prom': '推广券','reward': '打赏券','buy': '购买'}[value]
      },
      useRule (value) {
      	//使用规则
        return {'D': '和其他优惠券一起使用', 'M': '叠加使用'}[value]
      },
      type (value) {
      	//券类型
        return {'rebate': '折扣券', 'voucher': '抵用券', 'cash': '代金券', 'present': '礼品券', 'ticket': '票务'}[value]
      },
      couponUsedType(value) {
      	//电子券状态
        return {'default': '默认', 'share': '可分享', 'useonly': '只可使用'}[value]
      },
      couponType(value) {
      	//第三方券类型
        return {'default': '默认', 'third': '第三方'}[value]
      },
      thirdType(value) {
      	//使用描述类型
        return {'default': '默认', 'url': '链接'}[value]
      },
      couponStatus(value) {
      	//使用描述类型
        return {'00': '草稿', '01': '券已生效', '02': '过期', '03': '删除'}[value]
      },
    },
    created () {
      this.loadData()
    }
  }
</script>
